package com.wans.guli.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.hutool.core.bean.BeanUtil;
import com.wans.guli.product.vo.AttrAttrgroupRelationRestVO;
import com.wans.guli.product.vo.AttrGroupWithAttrRespVO;
import com.wans.guli.product.vo.AttrRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wans.guli.product.entity.AttrGroupEntity;
import com.wans.guli.product.service.AttrGroupService;
import com.wans.guli.common.utils.PageUtils;
import com.wans.guli.common.utils.R;


/**
 * 属性分组
 *
 * @author wans
 * @email wansconst@163.com
 * @date 2025-08-01 15:14:35
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list/{catId}")
    public R listById(@PathVariable Long catId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPageByCatId(catId, params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getInfoById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    @GetMapping("/{groupId}/attr/relation")
    public R attrByGroupId(@PathVariable("groupId") Long groupId) {
        List<AttrRespVO> data = attrGroupService.getAttrByGroupId(groupId);

        return R.success(data);
    }

    @GetMapping("/{attrGroupId}/noattr/relation")
    public R notRelationAttrByGroupId(@PathVariable("attrGroupId") Long groupId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.notRelationAttrByGroupId(groupId, params);

        return R.ok().put("page", page);
    }

    /**
     * 获取分类下所有分组&关联属性
     *
     * @param catelogId 分类id
     * @return 所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getWithAttr(@PathVariable("catelogId") Long catelogId) {
        // 获取分类下所有分组
        List<AttrGroupEntity> attrGroupEntities = attrGroupService.lambdaQuery()
                .eq(AttrGroupEntity::getCatelogId, catelogId).list();
        if (attrGroupEntities.isEmpty()) return R.ok();
        // 获取分组关联的所有属性
        List<AttrGroupWithAttrRespVO> data = attrGroupEntities.stream().map(item -> {
            List<AttrRespVO> attrRespVOS = attrGroupService.getAttrByGroupId(item.getAttrGroupId());
            AttrGroupWithAttrRespVO attrGroupWithAttrRespVO = BeanUtil.copyProperties(item, AttrGroupWithAttrRespVO.class);
            attrGroupWithAttrRespVO.setAttrs(attrRespVOS);
            return attrGroupWithAttrRespVO;
        }).collect(Collectors.toList());

        return R.success(data);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrAttrgroupRelationRestVO> relationRestVOS) {
        attrGroupService.saveBatchRelation(relationRestVOS);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 批量删除关联关系
     *
     * @param relationRestVOS 批量删除关联关系
     * @return 删除结果
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrAttrgroupRelationRestVO> relationRestVOS) {
        attrGroupService.deleteAttrRelation(relationRestVOS);

        return R.ok();
    }
}
